package cn.varin;

import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.junit.jupiter.api.Test;

public class MqttConnectionTest {



    /**
     *
     *
     * String serverURI,(mqtt服务端地址)
     * String clientId,(客户端id)
     * MqttClientPersistence persistence（内存持久类）
     *
     *
     */
    public static String serviceURL= "tcp://varin.cn:1883";
    public static String clientId="varya_test_01";
    public static MqttClient client;
    String user="varya";
    String password= "123456";

    static {
        try {
            // 建立一个mqqt客户端类
            client = new MqttClient(serviceURL,clientId,new MemoryPersistence());
        } catch (MqttException e) {
            throw new RuntimeException(e);
        }
    }


    // 创建mqtt连接
    @Test
    public void createConnectionTest() throws MqttException {
        MqttConnectOptions mqttConnectOptions = new MqttConnectOptions();
        mqttConnectOptions.setUserName(user);
        mqttConnectOptions.setPassword(password.toCharArray());

        client.connect(mqttConnectOptions);
//        注意：因为用test类方法执行的话，太快，可能看不出来，是否正真的建立的连接，所以添加一个死循环来保持程序的存在。
        while (true);
    }


}
